/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fctuserver;

/**
 *
 * @author ASUS
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author esha
 */
// Chat Server runs at port no. 9999
import java.io.*;
import java.util.*;
import java.net.*;
import static java.lang.System.out;
import src.org.multichat.server.ChatServer;
import utility.FCTUNetwork;
import utility.FCTUVoice;


public class  FCTUServer {
  Vector<String> users = new Vector<String>();
  Vector<HandleClient> clients = new Vector<HandleClient>();
  
 //Vector<String> gameopener = new Vector<String>();
  String opener = null;
  static int player_no=1;
  public void process() throws Exception  {
      ServerSocket server = new ServerSocket(9999,10);
      out.println("Network Server Started...");
      while( true) {
 		 Socket client = server.accept();
 		 HandleClient c = new HandleClient(client);
  		 clients.add(c);
     }  // end of while
  }
  
  
  public FCTUServer()
  {
  
  }
  
  
  public FCTUServer(int i)
  {
      try {
        Thread fn=new utility.FCTUNetwork();
        fn.start();
        
        Thread fv=new utility.FCTUVoice();
        fv.start();
      }catch (Exception e) {
			System.out.println(e);
			System.exit(0);
		}
      
  }
  
  
  
       
 
  
  
  
  public static void main(String ... args) throws Exception {
      
  try {
            new FCTUServer(1);
        } catch(Exception ex) {
            //out.println( "Error --> " + ex.getMessage());
        }

  }
      //new FCTUServer().process();
     // new ChatServer();} // end of main

  public void boradcast(String user, String message)  {
	    // send message to all connected users
	    for ( HandleClient c : clients )
	       if ( ! c.getUserName().equals(user) )
	          c.sendMessage(user,message);
  }

  class  HandleClient extends Thread {
        String name = "";
	BufferedReader input;
	PrintWriter output;

	public HandleClient(Socket  client) throws Exception {
         // get input and output streams
	 input = new BufferedReader( new InputStreamReader( client.getInputStream())) ;
	 output = new PrintWriter ( client.getOutputStream(),true);
	 // read name
	 name  = input.readLine();
         
         if (name.contains("%")) {
             
             if(opener == null) {
                 sendMessage(null, "Game Not started!.....");
             }
             else {
                 String[] val = name.split("%");
                 sendMessage(null, "Connect with " + opener + ".");
                 boradcast(name,val[0] + " has joined into the game........");
                 System.out.println("received a client");
                users.add(name); // add to vector
                System.out.println(name);
             }
                 
         }
         
         
         
//     for ( HandleClient c : clients )
//     {
//         if ( c.getUserName().equals(opener) )
//         {         
//                c.sendMessage("player"+player_no," is connected");
//                player_no++;
//         }
//         
//     }

	 start();
        }

        public void sendMessage(String uname,String  msg)  {
	    output.println(msg);
	}
        
        public void sendGS(String gs) {
            output.println(gs);
        }

        public String getUserName() {
            return name;
        }
        public void run()  {
    	     String line;
	     try    {
                while(true)   {
		 line = input.readLine();
		 if ( line.equals("end") ) {
		   clients.remove(this);
		   users.remove(name);
		   break;
                 }
         if(line.equals("OPENAGAME"))
         {
            //gameopener.add(this.name);
            opener=this.name;
            System.out.println("A Game open by : " + name);

            
           }
         else if (line.contains("%")) {
             
             if(opener == null) {
                 sendMessage(null, "Game Not started!.....");
             }
             else {
                 String[] val = line.split("%");
                 
                 boradcast(name,val[0] + " has joined into the game........");
             }
                 
         }
         else {
		 boradcast(name,line); // method  of outer class - send messages to all
         }
	       } // end of while
	     } // try
	     catch(Exception ex) {
	       System.out.println(ex.getMessage());
	     }
        } // end of run()
   } // end of inner class

} // end of Server

